{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Writing A Strategy\n",
    "--------------------\n",
    "The are a few things we should familiarise ourselves with before writing a new strategy.\n",
    "\n",
    "For example the source code for the strategy `Tit For Tat()` is shown below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import axelrod as axl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class TitForTat(axl.Player):\n",
    "    \"\"\"\n",
    "    A player starts by cooperating and then mimics previous move by\n",
    "    opponent.\n",
    "\n",
    "    Note that the code for this strategy is written in a fairly verbose\n",
    "    way. This is done so that it can serve as an example strategy for\n",
    "    those who might be new to Python.\n",
    "\n",
    "    Names\n",
    "\n",
    "    - Rapoport's strategy: [Axelrod1980]_\n",
    "    - TitForTat: [Axelrod1980]_\n",
    "    \"\"\"\n",
    "\n",
    "    # These are various properties for the strategy\n",
    "    name = 'Tit For Tat'\n",
    "    classifier = {\n",
    "        'memory_depth': 1,  # Four-Vector = (1.,0.,1.,0.)\n",
    "        'stochastic': False,\n",
    "        'inspects_source': False,\n",
    "        'manipulates_source': False,\n",
    "        'manipulates_state': False\n",
    "    }\n",
    "\n",
    "    def strategy(self, opponent):\n",
    "        \"\"\"This is the actual strategy\"\"\"\n",
    "        # First move\n",
    "        if len(self.history) == 0:\n",
    "            return C\n",
    "        # React to the opponent's last move\n",
    "        if opponent.history[-1] == D:\n",
    "            return D\n",
    "        return C"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What is required is to write the strategy method which takes an opponent as an argument. In the case of `TitForTat` the strategy checks if it has any history (`if len(self.history) == 0`). If it does not (ie this is the first play of the match) then it returns C. If not, the strategy simply repeats the opponent’s last move (`return opponent.history[-1]`):\n",
    "\n",
    "```python\n",
    "def strategy(opponent):\n",
    "    \"\"\"This is the actual strategy\"\"\"\n",
    "    # First move\n",
    "    if len(self.history) == 0:\n",
    "        return C\n",
    "    # Repeat the opponent's last move\n",
    "    return opponent.history[-1]\n",
    "```\n",
    "\n",
    "The variables C and D represent the cooperate and defect actions respectively."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Exercise\n",
    "--------\n",
    "\n",
    "- try an implement a strategy called Tit For 3 Tats\n",
    "- see how well it does in your own tournament\n",
    "\n",
    "\n",
    "Tit For 3 Tat rule:\n",
    "The strategy will start by cooperating and will defect only after the opponent has defected 3 times in a row."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once you have implemented the strategy which will look like this:\n",
    "    \n",
    "```python\n",
    "class TitFor3Tat(axl.Player):\n",
    "    ....\n",
    "```\n",
    "\n",
    "You can include it in a tournament by adding it to a list of players. For example:\n",
    "\n",
    "```python\n",
    "players = [TitFor3Tat(), axl.Cooperator(), axl.Random(), ...]\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Further Exercise\n",
    "----------------\n",
    "\n",
    "- Implement a strategy of your own. The strategy should follow the rules you believe could do well in an IPD tournament"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Contributing to the library\n",
    "----------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Anyone interested in contributing to the library should check the various tutorials for doing so at the documentation\n",
    "found here: http://axelrod.readthedocs.io/en/latest/tutorials/contributing/index.html?highlight=contributing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An important aspect of the library is documentation. Thus the first thing that is needed is a docstring that explains what the strategy does:\n",
    "\n",
    "`\"\"\"A player starts by cooperating and then mimics previous move by opponent.\"\"\"`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Secondly, any alternate names should be included and if possible references provided (this helps when trying to identify if a strategy has already been implemented or not):\n",
    "\n",
    "- Rapoport's strategy: [Axelrod1980]_\n",
    "- TitForTat: [Axelrod1980]_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These references can be found in the Bibliography (http://axelrod.readthedocs.io/en/latest/reference/bibliography.html). If a required references is not there please feel free to add it or just get in touch and we’d be happy to help.\n",
    "\n",
    "After that simply add in the string that will appear as the name of the strategy:\n",
    "\n",
    "`name = 'Tit For Tat'`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that this is mainly used in plots by matplotlib so you can use LaTeX if you want to. For example there is strategy with \\\\(\\pi\\\\) as a name:\n",
    "\n",
    "```python\n",
    "name = '$\\pi$'\n",
    "Following that you can add in the classifier dictionary:\n",
    "\n",
    "classifier = {\n",
    "    'memory_depth': 1,  # Four-Vector = (1.,0.,1.,0.)\n",
    "    'stochastic': False,\n",
    "    'inspects_source': False,\n",
    "    'manipulates_source': False,\n",
    "    'manipulates_state': False\n",
    "}\n",
    "```\n",
    "This helps classify the strategy as described in Classification (http://axelrod.readthedocs.io/en/latest/tutorials/advanced/classification_of_strategies.html#classification-of-strategies) of strategies."
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "game-python",
   "language": "python",
   "name": "game-python"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
